package com.share.auth.service;

import com.share.common.core.domain.R;
import com.share.common.core.exception.ServiceException;
import com.share.common.core.utils.StringUtils;
import com.share.system.api.model.LoginUser;
import com.share.user.api.RemoteUserInfoService;
import com.share.user.api.domain.UserInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class H5LoginService {
    @Autowired
    private RemoteUserInfoService remoteUserInfoService;
    public UserInfo login(String code) {

        // 用户名或密码为空 错误
        if (StringUtils.isAnyBlank(code))
        {
            //recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户/密码必须填写");
            throw new ServiceException("微信code必须填写");
        }
        // 查询用户信息
        R<UserInfo> userResult = remoteUserInfoService.wxLogin(code);
        // 登录失败
        if (StringUtils.isNull(userResult)) {
            System.out.println("User result is null");
        } else {
            System.out.println("User result data: " + userResult.getData());
        }
        if (StringUtils.isNull(userResult) || StringUtils.isNull(userResult.getData()))
        {
            throw new ServiceException("微信授权登录失败");
        }
        if (R.FAIL == userResult.getCode())
        {
            throw new ServiceException(userResult.getMsg());
        }

       UserInfo userInfo = userResult.getData();


        return userInfo;
    }

}
